﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>长时推送</title>
</head>
<body>
    <p id="msgContaner"></p>
    <script src="./signalr.js"></script>
    <script>
        let groupId = "longtimepush-group-2022";
        let msgContaner = document.querySelector("#msgContaner");
        let connection = new signalR.HubConnectionBuilder()
            .withUrl("/longtimepush")
            .build();
        connection.serverTimeoutInMilliseconds = 30 * 60 * 1000;

        connection.start().then(() => {
            console.log("开始链接");
            connection.send('JoinGroup', { id: connection.id, groupId: groupId });
        });

        connection.on("ClientHook", data => {
            console.log('客户端触发成功', data)
            msgContaner.innerHTML = `<code><pre>${JSON.stringify(data, true, 4)}</pre></code>`;
        });

        connection.onclose(() => {
            console.error("连接断开");
            setTimeout(() => {
                connection.start()
                    .then(() => {
                        console.info("重连成功！");
                        connection.send('JoinGroup', { id: connection.id, groupId: groupId });
                    })
                    .catch(err => {
                        console.error('SignalR Connection Error: ', err)
                        //alert("即时消息链接已断开，请重新刷新页面")
                    });
            }, 5000);
        });
    </script>
</body>
</html>